<?php
//if($strpos($reqPath, $myLocation) === 0) //ASsume true
//ReLative path
$relPath = substr($reqPath,strlen($myLocation), strlen($reqPath)-strlen($myLocation)-7);
//The 7 above is for .pnggif
$pngRel = realpath(".")."/".$relPath.".png";
$gifRel = realpath(".")."/".$relPath.".gif";


if ((strpos($_ENV['HTTP_USER_AGENT'], 'MSIE 6') !== false 
   || strpos($_ENV['HTTP_USER_AGENT'], 'MSIE 5') !== false)
   && strpos($_ENV['HTTP_USER_AGENT'], 'Opera') === false
   && strpos($_ENV['HTTP_USER_AGENT'], 'Gecko') === false
   && strpos($_ENV['HTTP_USER_AGENT'], 'Safari') === false) {
   // We are dealing with IE less than 7. Hooray for crap.	

   if(file_exists($gifRel)) {
      header("Content-type: image/gif");
      readfile($gifRel);
   }
   elseif(file_exists($pngRel)){
      $im = imagecreatefrompng($pngRel);
      $transparentColor = imagecolorallocate($im, 0xfe, 0x3, 0xf4 );
      //$ditherHistory = array(); //stores number of times it was transparent
      $height = imagesy($im);
      $width = imagesx($im);
      for($x = 0; $x < $width; $x++){
         for($y = 0; $y < $height; $y++) {
            $alpha = (imagecolorat($im,$x,$y) & 0x7F000000) >> 24;//127 is completely TRANSPARENT, 0 opaque
            //DITHER!
            if ($alpha > 3 && (
                  $alpha >=127-3 || 
                  (rand(0,127))>=(127-$alpha)
               )){
               imagesetpixel($im,$x,$y,$transparentColor);
            }

         }
      }
      imagecolortransparent($im, $transparentColor);
      imagegif($im, $gifRel);//save
      header("Content-type: image/gif");
      readfile($gifRel); //pass thru to browser

   } else {
      //Error.
      die("ERROR sorry, couldn't find ".$pngRel);
   }

} else {
   //Hooray! Good browser!
   if(file_exists($pngRel)){
      header("Content-type: image/png");
      readfile($pngRel); //pass thru to browser
   }
   else {
      //error
      die("ERROR sorry, couldn't find ".$pngRel);
   }
}
?>